package com.mediacloud.app.newsmodule.activity.microlive;

import android.view.View;
import android.widget.ImageView;

import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.model.news.kt.TypeX;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.microlive.Lives;
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 */

public class MicroLiveDetailItemOneImgViewHolder extends MicroLiveDetailItemBaseViewHolder
{

    public MicroLiveDetailItemOneImgViewHolder(View itemView, @IMicroDetailDataObj.LiveType int liveType)
    {
        super(itemView,liveType);
        hamapiImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try
                {
                    Lives data= v.getTag(R.id.appfacimage)!=null&&v.getTag(R.id.appfacimage) instanceof Lives? (Lives) v.getTag(R.id.appfacimage) :null;
                    if (data!=null&&data.getImage()!=null&&data.getImage().size()> 0 && v.getContext()!=null)
                    {
                        JSONArray images = new JSONArray();
                        for (String imgItem: data.getImage())
                        {
                            JSONObject item=new JSONObject();
                            item.put("src",imgItem);
                            images.put(images.length(),item);
                        }
                        final ArticleItem imgArticle = new ArticleItem();
                        imgArticle.setTitle(articleItem!=null?articleItem.getTitle():"");
                        imgArticle.setImage(images.toString());
                        imgArticle.setType(TypeX.NEWS.PHOTOS);
                        NewsItemClickUtils.OpenItemNewsHandle(v.getContext(), TypeX.NEWS.PHOTOS, imgArticle, new CatalogItem(), 0);
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
        });
    }
    @Override
    public void setItemData(Lives data)
    {
        super.setItemData(data);
        subLayout.setVisibility(View.VISIBLE);
        setSubLayoutSize();
        center_play.setVisibility(View.GONE);
        hamapiImage.setVisibility(View.VISIBLE);
        hamapiImage.setTag(R.id.appfacimage,data);
        try
        {
            hamapiImage.defaultBackGroundColor= AppFactoryGlobalConfig.getDefaultImagLoadBackground();
            hamapiImage.defaultDrawable=AppFactoryGlobalConfig.getDefaultImageLoadDrawable();
            hamapiImage.setScaleType(ImageView.ScaleType.FIT_XY);
            hamapiImage.load(data.getImage().get(0));
        }
        catch (Exception e)
        {
            hamapiImage.setDefaultRes(true);
        }
    }
}